
#pragma once

#include <spdlog/spdlog.h>
#include <vsg/all.h>

#include "./KeyBoardShortcuts.h"

#include <iostream>

namespace qbe::input {

class KeyBoardHandle : public vsg::Inherit<vsg::Visitor, KeyBoardHandle> {
 public:
  void apply(vsg::KeyPressEvent& e) override {
    key_press = true;
    key_press_event = &e;
  }

  void apply(vsg::KeyReleaseEvent& e) override {
    key_release = true;
    key_release_event = &e;
  }

  bool keyPressed(vsg::KeySymbol key_symbol) {
    // SPDLOG_INFO("keyPressed:");
    if (key_press) {
      return key_symbol == key_press_event->keyBase;
    }
    return false;
  }

  // 检测快捷键是否按下
  bool keyPressed(const KeyBoardShortcuts& kbs) {
    // SPDLOG_INFO("keyPressed:");
    if (key_press) {
      if (kbs.keyModifier != key_press_event->keyModifier)
        return false;

      // 非字符基础键
      if ((int)kbs.keyBase < 'a' && (int)kbs.keyBase > 'z' && (int)(int)kbs.keyBase < 'A' && (int)kbs.keyBase > 'Z')
        return kbs.keyBase == key_press_event->keyBase;

      // 字符基础键不区分大小写
      if ((int)kbs.keyBase <= 'z')
        return (kbs.keyBase == key_press_event->keyBase) || (kbs.keyBase + 32 == key_press_event->keyBase);
      else
        return (kbs.keyBase == key_press_event->keyBase) || (kbs.keyBase - 32 == key_press_event->keyBase);
    }
    return false;
  }

  bool keyReleased(vsg::KeySymbol key_symbol) {
    if (key_release) {
      return key_symbol == key_release_event->keyBase;
    }
    return false;
  }

  void reset() {
    key_press = false;
    key_release = false;
  }

  bool key_press{false};
  vsg::KeyPressEvent* key_press_event{nullptr};
  bool key_release{false};
  vsg::KeyReleaseEvent* key_release_event{nullptr};
};

}  // namespace qbe::input